import axios from 'axios';
import Router from '@/router/index'
import { getToken, log, LOCAL_USER } from "./user"
// 线上地址
const proUrl = "https://med.medte.com.cn";

export const baseApi = process.env.NODE_ENV === 'development'
    ? '/api/'
    : proUrl + "/api/org/";

const service = axios.create({
    // process.env.NODE_ENV === 'development' 来判断是否开发环境
    baseURL: baseApi,
    timeout: 5000
});

service.interceptors.request.use(
    config => {
        const token = getToken();
        log("req", config);
        if (token) {
            config.params.apiToken = token
        }
        log("req", config);
        return config;
    },
    error => {
        log(error);
        return Promise.reject();
    }
);

service.interceptors.response.use(
    response => {

        if (response.status === 200) {
            console.log("resLog", response.data)

            if (response.data.code == -1 || response.data.code == -100) {
                // 数据响应
                console.log("err data", response);
                // 删除登录信息
                localStorage.setItem(LOCAL_USER, null);
                Router.push({ path: '/login' })
            }
            return response.data;
        } else {
            Promise.reject();
        }
    },
    error => {
        log(error);
        return Promise.reject();
    }
);

export default service;
